package client

import (
	"context"
	"fmt"
	"github.com/2103A/message/order"
	"github.com/zeromicro/go-zero/zrpc"
)

func orderClient() order.OrderClient {
	conn := zrpc.MustNewClient(zrpc.RpcClientConf{
		Target: "dns:///127.0.0.1:8183",
	})
	return order.NewOrderClient(conn.Conn())
}

func OrderCreate(ctx context.Context, userID int64, goods []*order.PurchaseGoods) (string, error) {
	info, err := orderClient().CreateOrder(ctx, &order.CreateOrderRequest{
		UserId:        int32(userID),
		PurchaseGoods: goods,
	})
	if err != nil {
		return "", err
	}
	if info == nil {
		return "", fmt.Errorf("数据异常")
	}
	return info.PayUrl, nil
}

func OrderNotify(ctx context.Context, outTradeNo string) error {
	_, err := orderClient().OrderNotify(ctx, &order.OrderNotifyRequest{
		OutTradeNo: outTradeNo,
	})
	if err != nil {
		return err
	}
	return nil
}
